package engine.scene;

import engine.util.DataUtil;
import java.nio.FloatBuffer;
import math.vector.Vecmath;
import math.vector.vec3f;
import static org.lwjgl.openal.AL10.*;

public class SoundListener
{
    
    private vec3f listenerPos;
    //<editor-fold defaultstate="collapsed" desc="setPosition">
    public void setPosition(vec3f position)
    {
        this.listenerPos.setTo(position);
    }
    //</editor-fold>
    
    private vec3f listenerDir;
    //<editor-fold defaultstate="collapsed" desc="setDirection">
    public void setDirection(vec3f direction)
    {
        this.listenerDir.setTo(direction);
    }
    //</editor-fold>
    
    private vec3f listenerVel;
    //<editor-fold defaultstate="collapsed" desc="setVelocity">
    public void setVelocity(vec3f velocity)
    {
        this.listenerVel.setTo(velocity);
    }
    //</editor-fold>
    
    public SoundListener()
    {
        this.listenerPos = new vec3f();
        this.listenerDir = new vec3f(0.0f, 0.0f, -1.0f);
        this.listenerVel = new vec3f();
    }
    
    public void bind()
    {
        FloatBuffer orientation = DataUtil.get().reserveDataFloat(6);
        orientation.put(this.listenerDir.asArray()); // DIR vector
        orientation.put(Vecmath.AXIS_Y.asArray()); // UP vector
        orientation.flip();
        
        alListener3f(AL_POSITION, 
            this.listenerPos.x, 
            this.listenerPos.y, 
            this.listenerPos.z
        );
        alListener(AL_ORIENTATION, orientation);
        alListener3f(AL_VELOCITY, 
            this.listenerVel.x, 
            this.listenerVel.y, 
            this.listenerVel.z
        );
    }
    
    public void destroy()
    {
        this.listenerPos = null;
        this.listenerDir = null;
        this.listenerVel = null;
    }
    
}
